package jude.java.money.batch.task;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import jude.java.money.batch.Config;
import jude.java.money.batch.Definition;
import jude.java.money.batch.bean.Site;
import jude.java.money.batch.memory.Sites;

/**
 * 导出
 * */
public class Exporter implements Runnable {
	
	private static final String EXPORT_PATH = "E:\\batch\\export\\";
	private static final String CHONG_BLOG_FILE = EXPORT_PATH + "blogs.txt";
	private static final String CHONG_SITE_FILE = EXPORT_PATH + "sites.txt";
	private static final String HTML_NEWS_FILE = EXPORT_PATH + "news.txt";
	private static final String BOOKMARK_FILE = EXPORT_PATH + "bookmark.html";
	private static final String WP_DOMAINS_FILE = EXPORT_PATH + "domains.txt";
	private static final String WP_SPACES_FILE = EXPORT_PATH + "spaces.xls";
	
	@Override
	public void run() {
		out(exportHtmlOptions(), HTML_NEWS_FILE);
		out(exportSites2Chong(), CHONG_SITE_FILE);
		out(exportBlogs2Chong(), CHONG_BLOG_FILE);
		out(exportBookmarks(), BOOKMARK_FILE);
		out(exportDomais(), WP_DOMAINS_FILE);
		exportSpaceDomain(WP_SPACES_FILE);
	}
	
	private void out(String str, String file) {
		try {
			FileWriter out = new FileWriter(file);
			out.write(str);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 导出博客域名配置
	 */
	private String exportDomais() {
		String[][] sites = Config.Wordpress.SITES;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < sites.length; i++) {
			String[] group = sites[i];
			sb.append("\r\n\t");
			int c = 0;
			for (int j = 0; j < group.length; j++) {
				if (Config.isConfig(group[j]))
					continue;
				if (c != 0)
					sb.append(",");
				if ((c+1) % 5 == 0)
					sb.append("\r\n\t");
				sb.append("'").append(group[j]).append("'");
				c++;
			}
			sb.append("\r\n");
		}
		return sb.toString();
	}
	
	private void exportSpaceDomain(String fileName) {
		SpaceDomainExporter exporter = new SpaceDomainExporter(fileName);
		exporter.export();
	}
	
	private String exportHtmlOptions() {
		int n = 1;
		StringBuilder sb = new StringBuilder("\r\n");
		String[][] sites = Config.Zencart.SITES;
		Map<String, String> configs = new HashMap<String, String>();
		sb.append("<option value=\"http://localhost\" position=\"sidebox\" style=\"color:blue;\">0, localhost</option>\r\n");
		for (int i = 0; i < sites.length; i++) {
			for (int j = 0; j < sites[i].length; j++) {
				String site = sites[i][j];
				if (j == 0 && BatchTask.handleConfigs(configs, site)) {
					if (configs.containsKey(Definition.CONFIG_ABANDON) || configs.containsKey(Definition.CONFIG_IGNORE)
							|| configs.containsKey(Definition.CONFIG_2010))
						break;
					continue;
				}
				String news = Sites.getNews(site);
				if (Site.NEWS_BOTTOM.equals(news)) {
					sb.append("<option value=\"http://").append(site).append("\" position=\"bottom\">").append(n++).append(", ").append(site).append("</option>\r\n");
				} else if (Site.NEWS_SIDEBOX.equals(news)) {
					sb.append("<option value=\"http://").append(site).append("\" position=\"sidebox\" style=\"color:blue;\">").append(n++).append(", ").append(site).append("</option>\r\n");
				} else if (Site.NEWS_BOTH.equals(news)) {
					sb.append("<option value=\"http://").append(site).append("\" position=\"bottom\">").append(n++).append(", ").append(site).append("</option>\r\n");
					sb.append("<option value=\"http://").append(site).append("\" position=\"sidebox\" style=\"color:blue;\">").append(n++).append(", ").append(site).append("</option>\r\n");
				}
			}
		}
		sites = Config.Magento.SITES;
		if (sites.length > 0)
			sb.append("<!--magento-->\r\n");
		for (int i = 0; i < sites.length; i++) {
			for (int j = 0; j < sites[i].length; j++) {
				String site = sites[i][j];
				if (BatchTask.isConfig(site))
					continue;
				sb.append("<option value=\"http://").append(site).append("\">").append(n++).append(", ").append(site).append("</option>\r\n");
			}
		}
		return sb.toString();
	}
	
	private String exportBlogs2Chong() {
		StringBuilder sb = new StringBuilder();
		String[][] sites = Config.Wordpress.SITES;
		String[] users = {"uestrade","newton","superuser"};
		for (int i = 0; i < sites.length; i++) {
			for (int j = 0; j < sites[i].length; j++) {
				String site = sites[i][j];
				if (BatchTask.isConfig(site))
					continue;
				for (int k = 0; k < users.length; k++) {
					sb.append("http://www.").append(site).append("/\t").append(site).append("\t1002\t0\t").append(users[k]).append("\tues.com.cn\r\n");
				}
			}
		}
		return sb.toString();
	}
	
	private String exportBookmarks() {
		StringBuilder sb = new StringBuilder();
		sb.append("<!DOCTYPE NETSCAPE-Bookmark-file-1>\r\n"+
				"\r\n"+
				"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\r\n"+
				"<TITLE>UES Bookmarks</TITLE>\r\n"+
				"<H1>UES Bookmarks</H1>\r\n"+
				"<DL><p>\r\n"+
				"    <DT><H3 ADD_DATE=\"0\" LAST_MODIFIED=\"1303368925\" PERSONAL_TOOLBAR_FOLDER=\"true\">UES</H3>\r\n"+
				"    <DL><p>\r\n");
		exportBookmark(sb, Config.Zencart.SITES, "Zencart");
		exportBookmark(sb, Config.Wordpress.SITES, "Wordpress");
		exportBookmark(sb, Config.Magento.SITES, "Magento");
		sb.append("    </DL><p>\r\n"+
				"</DL><p>");
		return sb.toString();
	}
	
	private void exportBookmark(StringBuilder sb, String[][] sites, String mark) {
		sb.append("        <DT><H3 ADD_DATE=\"1290887954\" LAST_MODIFIED=\"1299025187\">").append(mark).append("</H3>\r\n");
		sb.append("        <DL><p>\r\n");
		for (int i = 0; i < sites.length; i++) {
			String[] group = sites[i];
			String name = "group" + (i+1);
			sb.append("            <DT><H3 ADD_DATE=\"1290887954\" LAST_MODIFIED=\"1299025187\">").append(name).append("</H3>\r\n");
			sb.append("            <DL><p>\r\n");
			for (int j = 0; j < group.length; j++) {
				String site = group[j];
				if (j == 0 && Config.isConfig(site))
					continue;
				sb.append("            <DT><A HREF=\"http://").append(site).append("/\" ADD_DATE=\"1290156194\">").append(site).append("</A>\r\n");
			}
			sb.append("            </DL><p>\r\n");
		}
		sb.append("        </DL><p>\r\n");
	}
	
	private String exportSites2Chong() {
		StringBuilder sb = new StringBuilder();
		String[][] sites = Config.Zencart.SITES;
		for (int i = 0; i < sites.length; i++) {
			for (int j = 0; j < sites[i].length; j++) {
				String site = sites[i][j];
				if (BatchTask.isConfig(site))
					continue;
				sb.append("keywords ").append("http://").append(site).append(" 1\r\n");
			}
		}
		sites = Config.Magento.SITES;
		for (int i = 0; i < sites.length; i++) {
			for (int j = 0; j < sites[i].length; j++) {
				String site = sites[i][j];
				if (BatchTask.isConfig(site))
					continue;
				sb.append("keywords ").append("http://").append(site).append(" 1\r\n");
			}
		}
		return sb.toString();
	}

	/* for ping
 http://www.websiteoutlook.com/{website}
 http://www.statbrain.com/{website}
 http://www.builtwith.com/?{website}
 http://snapshot.compete.com/{website}
 http://www.aboutus.org/{website}
 http://www.quantcast.com/{website}
 http://www.cubestat.com/{website}
 http://whois.tools4noobs.com/info/{website}
 http://www.alexa.com/siteinfo/{website}
 http://www.alexa.com/data/details/?url={website}
 http://www.siteadvisor.cn/sites/{website}/summary/
 http://whois.domaintools.com/{website}
 http://www.aboutdomain.org/backlinks/{website}/
 http://www.whoisya.com/{website}
 http://www.who.is/whois-com/{website}
 http://www.robtex.com/dns/{website}.html
 http://www.zimbio.com/search?q={website}&btnG=Search
 http://whois.ws/whois-info/ip-address/{website}/
 http://whoisx.co.uk/{website}
 http://searchanalytics.compete.com/s…s/{website}
	 * */
}
